package net.ibizsys.eclipse.studio.xml;

import java.io.File;
import java.util.Collections;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.ibizsys.eclipse.studio.Activator;
import net.ibizsys.eclipse.studio.data.IBizRuntimeType;
import net.ibizsys.eclipse.studio.data.IBizSystem;
import net.ibizsys.eclipse.studio.util.StringKit;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizProject extends XmlNode
{

	public static final String NODE_IBIZPROJECT = "IBIZPROJECT";

	/**
	 * 应用[App]类型项目
	 */
	public static final String TYPE_APP = "APP";

	/**
	 * 服务[SRV]类型项目
	 */
	public static final String TYPE_SRV = "SRV";

	/**
	 * 类型
	 */
	public static final String ATTR_TYPE = "TYPE";
	/**
	 * 代码名称
	 */
	public static final String ATTR_CODENAME = "CODENAME";

	/**
	 * 系统标识
	 */
	public static final String ATTR_SFID = "SFID";

	/**
	 * PUB项目名称
	 */
	public static final String ATTR_PUBPRJ = "PUBPRJ";

	/**
	 * USR项目名称
	 */
	public static final String ATTR_USRPRJ = "USRPRJ";

	/**
	 * 系统代码名
	 */
	public static final String ATTR_SFCODENAME = "SFCODENAME";

	/**
	 * 包代码名称
	 */
	public static final String ATTR_PKGCODENAME = "PKGCODENAME";
	
	/**
	 * 运行时类型
	 */
	public static final String ATTR_RUNTIMETYPE = "PFID";

	private IBizSysProxy system;
	private String codeName;
	private String type;
	private String sfID;
	private String sfCodeName;
	private String pubPRJName;
	private String usrPRJName;
	private String pkgCodeName;

	private IBizRuntimeType runtimeType;

	private IBizEntityList entityList = new IBizEntityList();
	private IBizModuleList moduleList = new IBizModuleList();
	private IBizAppModuleList appmoduleList = new IBizAppModuleList();
	private IBizViewList viewList = new IBizViewList();
	private IBizWFList wfList = new IBizWFList();
	private IBizCodeLists codeList = new IBizCodeLists();
	private IBizApplicationList appList = new IBizApplicationList();

	public IBizProject()
	{
	}

	public IBizProject(IBizSysProxy system)
	{
		this.system = system;
	}

	@Override
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_CODENAME, name, true))
		{
			this.codeName = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_TYPE, name, true))
		{
			this.type = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_SFID, name, true))
		{
			this.sfID = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_SFCODENAME, name, true))
		{
			this.sfCodeName = value;
			return;
		}

		if (StringKit.IsEqual(ATTR_PKGCODENAME, name, true))
		{
			this.pkgCodeName = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_PUBPRJ, name, true))
		{
			this.pubPRJName = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_USRPRJ, name, true))
		{
			this.usrPRJName = value;
			return;
		}
		
		if (StringKit.IsEqual(ATTR_RUNTIMETYPE, name, true))
		{
			this.runtimeType = IBizRuntimeType.convert(value);
			return;
		}	
		
		
		super.initAttribute(name, value);
	}

	@Override
	protected void initChildNode(Node node, IBizProject project)
	{
		if (StringKit.IsEqual(node.getNodeName(), IBizEntityList.NODE_PSDATAENTITIES, true))
		{
			this.entityList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizModuleList.NODE_PSMODULES, true))
		{
			this.moduleList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizAppModuleList.NODE_PSAPPMODULES, true))
		{
			this.appmoduleList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizViewList.NODE_PSAPPVIEWS, true))
		{
			this.viewList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizWFList.NODE_PSWORKFLOWS, true))
		{
			this.wfList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizCodeLists.NODE_PSCODELISTS, true))
		{
			this.codeList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizApplicationList.NODE_PSAPPLICATIONS, true))
		{
			this.appList.init(node, project);
			return;
		}
		super.initChildNode(node, project);
	}

	@Override
	public void sortChildren()
	{
		Collections.sort(entityList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(moduleList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(appmoduleList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(viewList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(wfList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(codeList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(appList, new XmlNodeTextComparator<XmlNode>());
	}

	public String getCodeName()
	{
		return codeName;
	}

	public String getType()
	{
		return type;
	}

	public IBizEntityList getEntityList()
	{
		return entityList;
	}

	public IBizModuleList getModuleList()
	{
		return moduleList;
	}

	public IBizAppModuleList getAppmoduleList()
	{
		return appmoduleList;
	}

	public IBizViewList getViewList()
	{
		return viewList;
	}

	public IBizWFList getWfList()
	{
		return wfList;
	}

	public IBizCodeLists getCodeList()
	{
		return codeList;
	}

	public String getSFID()
	{
		return sfID;
	}

	public String getSFCodeName()
	{
		return sfCodeName;
	}

	public String getPubPRJName()
	{
		return pubPRJName;
	}

	public String getUsrPRJName()
	{
		return usrPRJName;
	}

	public String getPkgCodeName()
	{
		return pkgCodeName;
	}
	
	public IBizRuntimeType getRuntimeType()
	{
		return runtimeType;
	}

	public IBizApplicationList getAppList()
	{
		return appList;
	}

	public IBizSystem getSystem()
	{
		if (this.system != null)
		{
			return this.system.getSystem();
		}
		return null;
	}

	public static IBizProject loadXmlFile(File file, IBizSysProxy system)
	{
		try
		{
			DocumentBuilder dbd = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = dbd.parse(file);

			Node node = doc.getFirstChild();

			IBizProject obj = new IBizProject(system);
			obj.init(node, obj);
			obj.sortChildren();

			return obj;
		} catch (Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取java代码根包
	 * 
	 * @return
	 */
	public String getJavaRootPath()
	{
		if (this.system != null)
		{
			return this.system.getJavaRootPath();
		}
		return "";
	}

	/**
	 * 获取Web目录绝对路径
	 * 
	 * @return
	 */
	public String getWebRootPath()
	{
		if (this.system != null)
		{
			return this.system.getWebRootPath();
		}
		return "";
	}

	public boolean isMavenProject()
	{
		String strFolder = String.format("%1$s/%2$s/src/main", Activator.getDefault().getRootPath(), getPubPRJName());
		File file = new File(strFolder);

		return file.exists();
	}
}
